﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class OnlineScans : System.Web.UI.Page
{
    int _serviceId = 0;
    private int pageumber = 0;
    private int _count = 0;
    private int _currCount = 0;
    private int _currCountNext = 0;
    ProxyWrapper.UserProxy.UserDetails _objUser = null;
    ProxyWrapper.ScansServiceWrapper _wrp = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                //LoadData();
                //_lstScan[0].
            }
        }
        catch (Exception ex)
        {

            throw;
        }
    }

    private List<ProxyWrapper.ScansProxy.Scans> LoadData()
    {
        _objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
        _wrp = new ProxyWrapper.ScansServiceWrapper();
        ProxyWrapper.ScansProxy.SearchParams _param = new ProxyWrapper.ScansProxy.SearchParams();
        _param.ClienId = _objUser.ClientId;
        _param.DeviceIdsList = Request["d"] != null ? Request["d"].ToString() : null;
        _param.FromDate = Request["sd"] != null ? Request["sd"].ToString() : null;
        _param.FromTime = Request["st"] != null ? Request["st"].ToString() : null;
        _param.HashCode = _objUser.HashCode;
        _param.ServiceIdsList = Request["s"] != null ? Request["s"].ToString() : null;

        string[] devices = Request["d[]"] != null ? Request["d[]"].ToString().Split(',') : null;
        string[] services = Request["s[]"] != null ? Request["s[]"].ToString().Split(',') : null;
        string[] users = Request["u[]"] != null ? Request["u[]"].ToString().Split(',') : null;

        string studentNum = Request["q"] != null ? Request["q"].ToString() : null;
        int stuNum = 0;
        if (int.TryParse(studentNum, out stuNum) && stuNum > 0)
        {
            _param.StudentIdNumber = stuNum;
        }
        else
        {
            _param.StudentName = studentNum;
            studentNum = studentNum.ToUpper();
        }
        string fromDateTIme = (Request["sd"] != null ? Request["sd"].ToString() : "") + " " + (Request["st"] != null ? Request["st"].ToString() : "");
        string toDatTIme = (Request["ed"] != null ? Request["ed"].ToString() : "") + " " + (Request["et"] != null ? Request["et"].ToString() : "");
        _param.ToDate = Request["ed"] != null ? Request["ed"].ToString() : null;
        _param.ToTime = Request["et"] != null ? Request["et"].ToString() : null;
        _param.UsersList = Request["u"] != null ? Request["u"].ToString() : null;
        List<ProxyWrapper.ScansProxy.Scans> _lstScan = _wrp.ListScans(_param);
        if (_lstScan != null && _lstScan.Count > 0)
        {
            _lstScan = _lstScan.Where(m => m.ServiceSubType != "ondevicedatabase" && m.ServiceSubType != "ondevicerecord").ToList<ProxyWrapper.ScansProxy.Scans>();
            if (_serviceId > 0 && _lstScan != null)
            {
                _lstScan = _lstScan.Where(m => m.ServiceId == _serviceId).ToList();
            }
            if (!string.IsNullOrWhiteSpace(fromDateTIme) && _lstScan != null)
            {
                DateTime _fromDate = Convert.ToDateTime(fromDateTIme);
                _lstScan = _lstScan.Where(m => Convert.ToDateTime(m.CreateDate) >= _fromDate).ToList();
            }
            if (!string.IsNullOrWhiteSpace(toDatTIme) && _lstScan != null)
            {
                DateTime _toDate = Convert.ToDateTime(toDatTIme);
                _lstScan = _lstScan.Where(m => Convert.ToDateTime(m.CreateDate) <= _toDate).ToList();
            }
            if (stuNum > 0 && _lstScan != null)
            {
                _lstScan = _lstScan.Where(m => m.StudentIdNumber == stuNum).ToList();
            }
            else if (!string.IsNullOrWhiteSpace(studentNum) && _lstScan != null)
            {
                _lstScan = _lstScan.Where(m => m.FirstName.ToUpper().Contains(studentNum.ToUpper()) || m.LastName.ToUpper().Contains(studentNum.ToUpper())).ToList();
            }
            IEnumerable<ProxyWrapper.ScansProxy.Scans> tmpScan = null;
            if (devices != null && _lstScan != null)
            {
                tmpScan = from order in _lstScan
                          where devices.Contains(order.DeviceId)
                          select order;
                if (tmpScan != null)
                    _lstScan = tmpScan.ToList<ProxyWrapper.ScansProxy.Scans>();
            }
            if (services != null && _lstScan != null)
            {
                tmpScan = from order in _lstScan
                          where services.Contains(order.ServiceId.ToString())
                          select order;
                if (tmpScan != null)
                    _lstScan = tmpScan.ToList<ProxyWrapper.ScansProxy.Scans>();
            }
            if (users != null && _lstScan != null)
            {
                tmpScan = from order in _lstScan
                          where users.Contains(order.UserId.ToString())
                          select order;
                if (tmpScan != null)
                    _lstScan = tmpScan.ToList<ProxyWrapper.ScansProxy.Scans>();
            }
            if (_lstScan != null && _lstScan.Count > 0)
            {
                _count = _lstScan.Count;
                _currCount = 1;
                _currCountNext = dataPaging.PageSize;
                if (Request.QueryString["pageNumber"] != null && Convert.ToInt32(Request.QueryString["pageNumber"].ToString()) > 0)
                {

                    pageumber = Convert.ToInt32(Request.QueryString["pageNumber"].ToString());
                }
                else
                {
                    pageumber = 1;
                }
                _currCountNext = dataPaging.PageSize * pageumber;
                //  _currCountNext = _currCountNext <= _count ? _currCountNext : _count;
                if (_currCountNext <= _count)
                {
                    _currCount = _currCountNext - dataPaging.PageSize + 1;
                }
                else
                {
                    _currCountNext = _count;
                    _currCount = (dataPaging.PageSize * (pageumber - 1)) + 1;
                }


            }
        }
        return _lstScan;
    }
    internal string GetName(string fname, string lname)
    {
        if (!string.IsNullOrWhiteSpace(fname) && !string.IsNullOrWhiteSpace(lname))
            return fname + " " + lname;
        else if (!string.IsNullOrWhiteSpace(fname))
            return fname;
        else if (!string.IsNullOrWhiteSpace(lname))
            return lname;
        else
            return " ";
    }
    internal string GetCount()
    {
        if (_count > 0)
            return "Showing 1 - " + _count.ToString() + " of " + _count.ToString() + " scans.";
        else
            return " ";


    }
    internal string GetValidityImage(int code)
    {
        switch (code)
        {
            case 1:
                return "Images/red.jpg";
            case 2:
                return "Images/green.jpg";
            case 3:
                return "Images/brown.jpg";
            case 4:
                return "Images/yellow.jpg";
            default:
                return "Images/red.jpg";
        }
    }
    protected void dataPaging_PreRender(object sender, EventArgs e)
    {
        try
        {
            lstvwScans.DataSource = LoadData();
            lstvwScans.DataBind();
        }
        catch (Exception)
        {
        }
    }
}